<?php

namespace App\Service\Kms\Common;

use App\Models\Common\ConfigModel;
use App\Models\Kms\KmsIosCode;
use App\Service\Kms\KmsBaseService;
use Illuminate\Support\Facades\Redis;
use Illuminate\Validation\ValidationException;

class ConfigService extends KmsBaseService
{
    const APP_TYPE_IOS     = "IOS";
    const APP_TYPE_ANDROID = "ANDROID";

    const APP_TYPE = [
        self::APP_TYPE_ANDROID => "安卓",
        self::APP_TYPE_IOS     => "ios"
    ];

    const APP_TYPE_MAPPING_TABLE_TYPE = [
        self::APP_TYPE_ANDROID => "appVersionAndroid",
        self::APP_TYPE_IOS     => "appVersionIos"
    ];

    const APP_NAME_TYPE_KMS_APP = 3;

    const APP_NAME_TYPE_MAP = [
        self::APP_NAME_TYPE_KMS_APP => '夸父运营',
    ];

    /**
     * @param object $params
     * @return bool[]
     * @throws ValidationException
     */
    public static function noLogin(object $params): array
    {
        $virtualLogin = \App\Service\OpenShop\Feishu\ConfigService::getConfigDetail("kms.auth", "virtualLogin", 5 * 60);
        return [
            "virtualLogin" => $virtualLogin["value"]["version"] == $params->version && $virtualLogin["value"]["switch"]
        ];
    }

    /**
     * 获取夸父运营app下载链接
     * @param string $appType
     * @param string $nowVersion
     * @param int $appNameType
     * @return array[]
     */
    public static function getAppVersionInfo(string $appType, string $nowVersion, int $appNameType): array
    {
        $appVersion = ConfigModel::query()
            ->where("module", "kms")
            ->where("name", self::APP_TYPE_MAPPING_TABLE_TYPE[$appType])
            ->value('value');

        $appVersion = json_decode($appVersion, true);
        if ($appVersion && $appVersion['nowVersion'] <= $nowVersion && $appVersion['appNameType'] == $appNameType) {
            $appVersion = new \stdClass();
            return ['versionInfo' => $appVersion];
        }
        if ($appType == self::APP_TYPE_IOS) {
            $appVersion['downloadUrl'] = self::getIosCode(false)['url'];
        }
        return ['versionInfo' => $appVersion];
    }

    /**
     * 夸父运营ios code使用记录
     * @param string $code
     * @return array
     */
    public static function iosCodeUse(string $code): array
    {
        $info = KmsIosCode::info($code);
        if (!empty($info)) {
            KmsIosCode::query()
                ->where('id', $info->id)
                ->update(['status' => KmsIosCode::STATUS_USE]);
        }
        return [];
    }

    /**
     * 夸父运营ios code
     * @param bool $isDel
     * @return array
     */
    public static function getIosCode($isDel = true): array
    {
        $code  = '';
        $count = Redis::sCard(KmsIosCode::KMS_IOS_DOWNLOAD_CODE);// 先查询redis中数据是否存在 否则直接查询数据库
        if ($count) {
            $code = Redis::sPop(KmsIosCode::KMS_IOS_DOWNLOAD_CODE);
        }
        $info = KmsIosCode::info($code);
        if (!empty($info) && $isDel) {
            KmsIosCode::query()
                ->where('id', $info->id)
                ->update(['status' => KmsIosCode::STATUS_USE, 'updatedAt' => time()]);
        }
        return [
            'code' => $info->code ?? "",
            'url'  => $info->url ?? "",
        ];
    }
}
